home *** CD-ROM | disk | FTP | other *** search
- /*
- * Printer.c - Routines for printing.
- * Mark D. Rustad 86/06/06.
- */
-
- /*
- * Include files.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <Clister.h>
-
- #include <Types.h>
- #include <CursorCtl.h>
- #include <Memory.h>
- #include <Errors.h>
- #include <Files.h>
- #include <Fonts.h>
- #include <PrintTraps.h>
- #include <Resources.h>
- #include <Windows.h>
-
- _TITLE("Printer.c - Routines for printing.")
- _SPACE("4,10,definitions")
- /*
- * Include global definitions.
- */
-
- #include "glob.h"
-
-
- /*
- * Private definitions.
- */
-
- struct pinfo /* Page information structure */
- {
- char *titlp; /* Pointer to title for page */
- char *stitp; /* Pointer to subtitle for page */
- char *sstitp; /* Pointer to subsubtitle for page */
- long fpos; /* File position for page */
- long lineno; /* Starting line number for page */
- };
-
-
- /*
- * Private globals.
- */
-
- static THPrint prRecHdl; /* Handle to print record */
- static TPPrPort myPrPort; /* Pointer to print port */
- static Rect border; /* Rectangle of page */
- static Rect TxtRect; /* Rectangle for text */
-
- static long Last_pos; /* Last file position */
- static struct pinfo *Pinfp = 0L; /* Pointer to Pinfo array */
- static short HdrLineHgt; /* Header line height */
- static short HdrLineStart; /* Header line starting point */
- static short TxtLineHgt; /* Text line height */
- static short TxtLineStart; /* Text line starting point */
- static short Lineno; /* Number of lines on page */
- static short Pointctr; /* Point counter */
- static short Pageno; /* Page number */
- static short Pagesz; /* No. of lines on a page */
- static short Flineno; /* Line number in file */
- static short Blklns; /* No. of blank lines skipped */
- static char *Dtime; /* Points to date/time string */
-
- _SPACE("4,10,print_job")
- /*
- * print_job - Print selected files.
- *
- * Calls: dofile, fclose, fopen, memset, NewHandle, PrClose, PrintDefault,
- * PrOpen, PrOpenDoc
- */
-
- void print_job()
- {
- short i;
- TPrStatus myStRec;
- THPrint ResHdl;
- Rect windowrect;
- WindowRecord wrec;
- FMetricRec firec;
- short MyRFNum, RFNum;
- void dofile();
-
- SetFractEnable(1); /* Enable fractional spacing */
- windowrect.top = windowrect.left = 1;
- windowrect.bottom = windowrect.right = 2;
- strcpy(Tmpstr, Finfo[0].fname);
- if (Nfiles > 1)
- strcat(Tmpstr, ", et. al.");
- newwindow((Ptr)&wrec, &windowrect, Tmpstr, 1, plainDBox, (WindowPtr)-1, 0, 0);
-
- MyRFNum = CurResFile();
- if ((prRecHdl = ResHdl = (THPrint)Get1Resource('PSup', 128)) == 0)
- prRecHdl = (THPrint)NewHandle(sizeof(TPrint));
- PrOpen();
- if (ResHdl == 0)
- PrintDefault(prRecHdl);
- if (PSOpt)
- {
- if (PrStlDialog(prRecHdl))
- {
- RFNum = CurResFile();
- if (ResHdl == 0)
- {
- UseResFile(MyRFNum);
- AddResource((Handle)prRecHdl, 'PSup', 128, "\p");
- UseResFile(RFNum);
- }
- else
- {
- ChangedResource((Handle)ResHdl);
- }
- UpdateResFile(MyRFNum);
- }
- }
- else
- PrValidate(prRecHdl);
-
- // if (PrJobDialog(prRecHdl))
- {
- Fixed ftmp;
-
- myPrPort = PrOpenDoc(prRecHdl, 0, 0);
- border = (**prRecHdl).prInfo.rPage;
- TextFont(HFNum);
- TextSize(HFSize);
- FontMetrics(&firec);
- ftmp = ((firec.ascent + firec.descent + firec.leading) * LSOpt) / 100;
- HdrLineHgt = ((ftmp + 0x8000) >> 16) & 0xFFFF;
- ftmp = firec.ascent + 0x8000;
- HdrLineStart = border.top + ((ftmp >> 16) & 0xFFFF) + (2 + 2);
- if (HOpt)
- TxtLineStart = (3 * HdrLineHgt) + (2 + 2) + (2 + 2);
- else
- TxtLineStart = (2 + 2);
-
- TextFont(TFNum);
- TextSize(TFSize);
- FontMetrics(&firec);
- ftmp = ((firec.ascent + firec.descent + firec.leading) * LSOpt) / 100;
- TxtLineHgt = ((ftmp + 0x8000) >> 16) & 0xFFFF;
- ftmp = firec.ascent + 0x8000;
- TxtLineStart += border.top + ((ftmp >> 16) & 0xFFFF);
-
- Pagesz = (border.bottom - TxtLineStart - (2 + 2)) / TxtLineHgt;
- if (ROpt)
- {
- for (i = Nfiles - 1; i >= 0; --i) /* Print in reverse order */
- {
- if ((Fd = fopen(Finfo[i].fname, "r")) == NULL)
- continue;
-
- dofile(Finfo + i);
- fclose(Fd);
- free(Finfo[i].fname); /* Free space used by file name */
- Finfo[i].fname = 0;
- }
- }
- else
- {
- for (i = 0; i < Nfiles; ++i) /* Print in forward order */
- {
- if ((Fd = fopen(Finfo[i].fname, "r")) == NULL)
- continue;
-
- dofile(Finfo + i);
- fclose(Fd);
- free(Finfo[i].fname); /* Free space used by file name */
- Finfo[i].fname = 0;
- }
- }
- PrCloseDoc(myPrPort);
- if (PrError() == noErr && (*prRecHdl)->prJob.bJDocLoop == bSpoolLoop)
- PrPicFile(prRecHdl, 0, 0, 0, &myStRec);
- }
-
- PrClose();
- CloseWindow((WindowPtr)&wrec);
- }
-
- _SPACE("4,10,dofile")
- /*
- * dofile - Process one file.
- *
- * Inputs:
- * fip (param) - finfo pointer.
- * Fd - File descriptor.
- * Pagesz - No. of lines on page.
- *
- * Calls: countit, fgets, free, fseek, gettime, ppd, printit,
- * printline, sprintf, strncpy, tscan
- */
-
- static void dofile(fip)
- struct finfo *fip;
- {
- int i, j; /* Loop index */
- short pix; /* Pinfo index */
- char *gettime();
- void countit(), tscan();
- extern char *calloc();
-
- Dtime = gettime(); /* Get edited ASCII time */
- Blklns = Pageno = 0;
- Lineno = Pagesz; /* Force eject on first printable line */
-
- if (Init_title[0] == '\0') /* If title not set by -t */
- strncpy(Init_title, fip->fname, TITLESIZE); /* Use filename as title */
-
- tscan(); /* Scan up to 100 lines for _TITLE */
- strncpy(Title, Init_title, TITLESIZE);
-
- Pinfp = (struct pinfo *)calloc(MAXPAGES + 1, sizeof(struct pinfo));
- if (Pinfp == 0L) /* If calloc fails, give up */
- {
- Init_title[0] = Subtitle[0] = Subsubtitle[0] = '\0'; /* Clear Title */
- return;
- }
-
- Last_pos = 0;
- for (Flineno = 1; fgets(LineBuf, MAXLINE, Fd) != 0;
- ++Flineno, Last_pos = ftell(Fd))
- {
- for (i = 0; isspace(LineBuf[i]); ++i)
- if (LineBuf[i] == '\f')
- {
- LineBuf[i] = ' ';
- Lineno = Pagesz; /* Force eject */
- }
- if (LineBuf[i] == '\0')
- {
- ++Blklns; /* Count blank line skipped */
- continue; /* Skip blank line */
- }
-
- for (j = i; LineBuf[j]; ++j) /* Check for form feed on line */
- if (LineBuf[j] == '\f')
- {
- LineBuf[j] = ' ';
- Lineno = Pagesz; /* Force eject */
- }
-
- if (LineBuf[i] == '_' && ppd(&LineBuf[i])) /* If directive */
- Blklns = 0; /* Ignore blank lines before directive */
- else
- countit(); /* Print non-directive line */
- }
-
- if (Pageno != 0) /* If anything to print */
- {
- if (ROpt)
- {
- for (pix = Pageno; pix > 0; --pix)
- dopage(pix, fip);
- }
- else
- {
- for (pix = 1; pix <= Pageno; ++pix)
- dopage(pix, fip);
- }
-
- /* Free any remaining title, subtitle or sub-subtitle strings. */
-
- if (Pinfp[0].titlp != 0L)
- free(Pinfp[0].titlp);
- if (Pinfp[0].stitp != 0L)
- free(Pinfp[0].stitp);
- if (Pinfp[0].sstitp != 0L)
- free(Pinfp[0].sstitp);
- }
-
- free(Pinfp);
- Init_title[0] = Subtitle[0] = Subsubtitle[0] = '\0'; /* Clear Title */
- }
-
- _SPACE("4,20,countit")
- /*
- * countit - Count line.
- *
- * Counts printable lines during the first pass of laser printing and
- * sets up Pinfp entries for each page.
- *
- * Inputs:
- * Flineno - Current line number in file.
- * Last_pos - File position for start of this line.
- * Subsubtitle - Pointer to subsubtitle string.
- * Subtitle - Pointer to subtitle string.
- * Title - Pointer to title string.
- *
- * Throughputs:
- * Blklns - Cleared.
- * Lineno - Incremented to indicate current line on page.
- * Pageno - Incremented for each new page.
- * Pinfp - Updated for each page.
- *
- * Calls: upd_str_ptr
- */
-
- static void countit()
- {
- char *upd_str_ptr();
-
- if (++Lineno + Blklns >= Pagesz)
- {
- Lineno = 1;
- Blklns = 0;
-
- if (Pageno < MAXPAGES) /* File will be truncated if > MAXPAGES */
- {
- Pinfp[Pageno+1].titlp = upd_str_ptr(Pinfp[Pageno].titlp, Title);
- Pinfp[Pageno+1].stitp = upd_str_ptr(Pinfp[Pageno].stitp, Subtitle);
- Pinfp[Pageno+1].sstitp = upd_str_ptr(Pinfp[Pageno].sstitp, Subsubtitle);
-
- ++Pageno;
- SpinCursor(-4);
- Pinfp[Pageno].fpos = Last_pos;
- Pinfp[Pageno].lineno = Flineno;
- }
- return;
- }
-
- Lineno += Blklns; /* Advance Lineno for skipped blank lines */
-
- Blklns = 0;
- }
-
- _SPACE("4,20,dopage")
- /*
- * dopage - Print one page.
- */
-
- static dopage(pix, fip)
- int pix; /* Page index */
- struct finfo *fip;
- {
- int i, j;
- void printit(), printline();
- extern long fseek();
-
- SpinCursor(4);
- PrOpenPage(myPrPort, 0);
- if (BOpt)
- {
- PenSize(2, 2);
- FrameRoundRect(&border, 16, 16);
- if (HOpt)
- {
- MoveTo(border.left, TxtLineStart - 4);
- LineTo(border.right, TxtLineStart - 4);
- }
- }
-
- PenSize(1, 1);
- if (HOpt)
- {
- TextFont(HFNum);
- TextSize(HFSize);
-
- if (Pinfp[pix].titlp)
- {
- MoveTo(border.left + 8, HdrLineStart);
- drawstring(Pinfp[pix].titlp);
- }
- sprintf(Tmpstr, "%s Page %d of %d", Dtime, pix, Pageno);
- MoveTo(border.right - stringwidth(Tmpstr) - 8, HdrLineStart);
- drawstring(Tmpstr);
-
- if (Pinfp[pix].stitp)
- {
- MoveTo(border.left + 8, HdrLineStart + HdrLineHgt);
- drawstring(Pinfp[pix].stitp);
- }
- if (POpt)
- {
- MoveTo(border.right - StringWidth(*Prop_str) - 8, HdrLineStart + HdrLineHgt);
- DrawString(*Prop_str);
- }
-
- if (Pinfp[pix].sstitp)
- {
- MoveTo(border.left + 8, HdrLineStart + HdrLineHgt + HdrLineHgt);
- drawstring(Pinfp[pix].sstitp);
- }
- }
-
- TextFont(TFNum);
- TextSize(TFSize);
- Lineno = Blklns = 0;
- Pointctr = TxtLineStart;
-
- /* Position to start of page */
- fseek(Fd, Pinfp[pix].fpos, 0);
-
- /* Print page */
- for (Flineno = Pinfp[pix].lineno;
- fgets(LineBuf, MAXLINE, Fd) != 0; ++Flineno)
- {
- for (i = 0; isspace(LineBuf[i]); ++i)
- if (LineBuf[i] == '\f')
- {
- LineBuf[i] = ' ';
- Lineno = Pagesz; /* Force eject */
- }
- if (LineBuf[i] == '\0')
- {
- ++Blklns; /* Count blank line skipped */
- continue; /* Skip blank line */
- }
-
- for (j = i; LineBuf[j]; ++j) /* Check for form feed on line */
- if (LineBuf[j] == '\f')
- {
- LineBuf[j] = ' ';
- Lineno = Pagesz; /* Force eject */
- }
-
- if (LineBuf[i] == '_' && ppd(&LineBuf[i])) /* If directive */
- Blklns = 0; /* Ignore blank lines before directive */
- else
- {
- if (++Lineno + Blklns >= Pagesz)
- {
- int offset;
-
- offset = 1 - (ROpt * 2);
- if (Pinfp[pix+offset].titlp != Pinfp[pix].titlp
- && Pinfp[pix].titlp)
- free(Pinfp[pix].titlp);
- if (Pinfp[pix+offset].stitp != Pinfp[pix].stitp
- && Pinfp[pix].stitp)
- free(Pinfp[pix].stitp);
- if (Pinfp[pix+offset].sstitp != Pinfp[pix].sstitp
- && Pinfp[pix].sstitp)
- free(Pinfp[pix].sstitp);
- break;
- }
- printit(fip); /* Print non-directive line */
- }
- }
-
- PrClosePage(myPrPort);
- }
-
- _SPACE("4,25,ppd")
- /*
- * ppd - Process possible directive.
- *
- * Inputs:
- * buf - (param) Address of possible directive.
- * Laser - Flag indicating laser printing.
- * Pagesz - Length of page, in lines.
- *
- * Outputs: Returns true if directive processed.
- *
- * Throughputs:
- * Lineno - Line number on current page may be incremented
- * to allow for whitespace output by this routine or may be
- * set to a large value to force a page eject later in
- * printit.
- * Title - The current title will be changed in processing of
- * a _TITLE directive.
- * Subtitle - The current subtitle will be changed in
- * processing of a _SUBTITLE directive.
- * Subsubtitle - The current sub-subtitle will be changed in
- * processing of a _SPACE directive if the optional third
- * argument is present.
- *
- * Calls: collect, sscanf, strncmp.
- */
-
- static ppd(buf)
- char buf[];
- {
- long lcnt, scnt;
- char item[TITLESIZE+2];
-
- if (strncmp(buf, "_SPACE", 6) == 0) /* If SPACE directive */
- {
- collect(item, &buf[6]);
- lcnt = 0;
- Subsubtitle[0] = '\0';
- item[strlen(item)] = '\n';
-
- #ifdef BSD
- if (sscanf(item, "%D , %D , %60s", &scnt, &lcnt, Subsubtitle))
- #else
- if (sscanf(item, "%ld , %ld , %60[^\"),\n]", &scnt, &lcnt, Subsubtitle))
- #endif
- {
- Subsubtitle[TITLESIZE+1] = '\0';
- if (lcnt + scnt + Lineno + 1 >= Pagesz)
- Lineno = Pagesz; /* Force eject */
- else
- {
- if (scnt < 0)
- scnt = 0;
- Lineno += scnt;
- Pointctr += TxtLineHgt * scnt;
- }
- }
- return true;
- }
- else if (strncmp(buf, "_SUBTITLE", 9) == 0) /* Else if SUBTITLE */
- {
- Subsubtitle[0] = '\0';
- collect(Subtitle, &buf[9]);
- Lineno = Pagesz; /* Force eject */
- return true;
- }
- else if (strncmp(buf, "_TITLE", 6) == 0) /* Else if TITLE */
- {
- collect(Title, &buf[6]);
- return true;
- }
- else if (strncmp(buf, "_EJECT", 6) == 0 ||
- strncmp(buf, "_PAGE", 5) == 0)
- {
- Subsubtitle[0] = '\0';
- Lineno = Pagesz; /* Force eject */
- return true;
- }
-
- return false; /* If no valid directive found */
- }
-
- _SPACE("4,20,printit")
- /*
- * printit - Print line.
- *
- * Prints non-blank line from file adding line numbers and handles counted
- * blank lines.
- *
- * Inputs:
- * fip - (param) Pointer to file info structure.
- * Flineno - Current line number in file.
- *
- * Throughputs:
- * Lineno - Incremented to indicate current line on page.
- * Blklns - Cleared.
- *
- * Calls: expand_tabs, PrCtlCall, printline, sprintf
- */
-
- static void printit(fip)
- struct finfo *fip;
- {
- int i; /* Loop index */
- int len; /* Length of string */
- void printline();
- extern char *expand_tabs();
-
- Lineno += Blklns; /* Advance Lineno for skipped blank lines */
- for (i = 0; i < Blklns; ++i) /* Print skipped blank lines, if any */
- {
- Pointctr += TxtLineHgt;
- if (NOpt)
- {
- MoveTo(border.left + 8, Pointctr);
- len = sprintf(Tmpstr, "%5d", Flineno - Blklns + i);
- drawstring(Tmpstr);
- }
- }
-
- Blklns = 0;
-
- Pointctr += TxtLineHgt;
- MoveTo(border.left + 8, Pointctr);
-
- len = (NOpt) ? sprintf(Tmpstr, "%5d ", Flineno) : 0;
-
- expand_tabs(Tmpstr + len, LineBuf, fip->ntabs) - Tmpstr;
- drawstring(Tmpstr);
- }
-
- _SPACE("4,10,upd_str_ptr")
- /*
- * upd_str_ptr - Update string pointer.
- *
- * Returns the previous string pointer if str is the same string, otherwise
- * the new string is copied to a block in the heap and a pointer to the block
- * is returned.
- *
- * Inputs:
- * oldp - (param) Pointer to previous string.
- * str - (param) Pointer to string.
- *
- * Outputs:
- * Returns value of new pointer, 0L if appropriate or malloc failed.
- *
- * Calls: malloc, strcmp, strcpy, strlen
- */
-
- static char *upd_str_ptr(oldp, str)
- char *oldp;
- char *str;
- {
- char *p;
- extern char *malloc();
-
- if (oldp && strcmp(oldp, str) == 0)
- return oldp;
- else
- {
- if (*str == '\000')
- return 0L;
- else
- {
- p = malloc(strlen(str)+1);
- if (p == 0L)
- return 0L;
- strcpy(p, str);
- return p;
- }
- }
- }
-
-
- /* End of laser.c */